home *** CD-ROM | disk | FTP | other *** search
/ Amiga News 95 / Amiga News 95.iso / dpat / dpat06 / itex / iconify.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-04  |  4.6 KB  |  219 lines

  1. /*  :ts=8 bk=0
  2.  *
  3.  * iconify.c:    You asked for it, you got it.
  4.  *
  5.  * Copyright 1987 by Leo L. Schwab.
  6.  * Permission is hereby granted for use in any and all programs, both
  7.  * Public Domain and commercial in nature, provided this Copyright notice
  8.  * is left intact.  Purveyors of programs, at their option, may wish observe
  9.  * the following conditions (in the spirit of hackerdom):
  10.  *    1: You send me a free, registered copy of the program that uses the
  11.  *       iconify feature,
  12.  *    2: If you're feeling really nice, a mention in the program's
  13.  *       documentation of my name would be neat.
  14.  *
  15.  *                     8712.10        (415) 456-3960
  16.  */
  17. #include <exec/types.h>
  18. /* #include <devices/timer.h> */
  19. #include <intuition/intuition.h>
  20. #define    ICON_IMAGE    0
  21. #define    ICON_BORDER    1
  22. #define    ICON_FUNCTION    2
  23.  
  24. /*  Suggested icon size for a standard (640 x 200) WorkBench screen.  */
  25. #define    ICONWIDTH    ((UWORD) 20)
  26. #define    ICONHEIGHT    ((UWORD) 10)
  27.  
  28.  
  29. /*
  30.  * It is recommended that the tick rate not be made too rapid to avoid
  31.  * bogging down the system.
  32.  */
  33. #define    TICKS_PER_SECOND    10
  34.  
  35. /*
  36.  * Some programmers may not wish to have the added functionality of the
  37.  * ICON_FUNCTION feature.  If you're such a programmer, you may comment out
  38.  * the following #define, which will eliminate the code to handle function
  39.  * calls, and make iconify() even smaller.
  40.  */
  41. /* #define    USE_FUNCTIONS */
  42.  
  43. /*
  44.  * Jim Mackraz suggested making icons easily identifiable by outside
  45.  * programs, so this constant gets stuffed into the UserData field.
  46.  */
  47. #define    ICON    0x49434f4eL        /*  'ICON'  */
  48.  
  49.  
  50. extern void    *OpenWindow(), *GetMsg(), *CreatePort(), *CreateExtIO(),
  51.         *CheckIO();
  52. extern long    OpenDevice(), DoubleClick();
  53.  
  54.  
  55. static struct Gadget gadget = {
  56.     NULL,
  57.     0, 0, 0, 0,
  58.     NULL,                /*  Set later  */
  59.     GADGIMMEDIATE,
  60.     WDRAGGING,            /*  Observe the Magic!  */
  61.     NULL,                /*  Set later  */
  62.     NULL, NULL, NULL, NULL,
  63.     0, 0
  64. };
  65.  
  66. static struct NewWindow windef = {
  67.     0, 0, 0, 0,            /*  Set later  */
  68.     -1, -1,
  69.     GADGETDOWN,
  70.     BORDERLESS | SMART_REFRESH | NOCAREREFRESH,
  71.     &gadget,
  72.     NULL, NULL, NULL, NULL,        /*  Lotsa these  */
  73.     0, 0, 0, 0,
  74.     WBENCHSCREEN
  75. };
  76.  
  77. static struct Window        *win;
  78.  
  79. #ifdef USE_FUNCTIONS
  80. static struct timerequest    *tr;
  81. static struct MsgPort        *reply;
  82. #endif
  83.  
  84.  
  85. iconify (left, top, width, height, screen, ptr, type)
  86. UWORD *left, *top, width, height;
  87. struct Screen *screen;
  88. APTR ptr;
  89. int type;
  90. {
  91.     register struct IntuiMessage    *msg;
  92.     long                secs = 0, mics = 0,
  93.                     cs, cm,
  94.                     class,
  95.                     sigmask;
  96.  
  97.     windef.LeftEdge        = *left;
  98.     windef.TopEdge        = *top;
  99.     windef.Width        = width;
  100.     windef.Height        = height;
  101.     windef.Type = (windef.Screen = screen) ? CUSTOMSCREEN : WBENCHSCREEN;
  102.  
  103.     gadget.Flags        = GADGHCOMP | GRELWIDTH | GRELHEIGHT;
  104.  
  105.     switch (type & 3) {
  106.     case ICON_IMAGE:
  107.         gadget.Flags        |= GADGIMAGE;
  108.     case ICON_BORDER:
  109.         gadget.GadgetRender    = ptr;
  110.         break;
  111.  
  112.     case ICON_FUNCTION:
  113. #ifdef USE_FUNCTIONS
  114.         gadget.GadgetRender    = NULL;
  115. #else
  116.         return (0);
  117. #endif
  118.         break;
  119.  
  120.     default:
  121.         return (0);
  122.     }
  123.  
  124.     if (!openstuff ())
  125.         return (0);
  126.     sigmask = 1L << win -> UserPort -> mp_SigBit;
  127.  
  128. #ifdef USE_FUNCTIONS
  129.     if (type == ICON_FUNCTION) {
  130.         sigmask |= 1L << reply -> mp_SigBit;
  131.         tr -> tr_node.io_Command= TR_ADDREQUEST;
  132.         tr -> tr_time.tv_secs    = 0;
  133.         tr -> tr_time.tv_micro    = (1000000L / TICKS_PER_SECOND);
  134.         SendIO (tr);
  135.         /*
  136.          * Make initialization call to user's function.
  137.          * Isn't typecasting wonderful?  :-|
  138.          */
  139.         (* ((void (*)()) ptr)) (win, (WORD) 1);
  140.     }
  141. #endif
  142.  
  143.     while (1) {
  144.         Wait (sigmask);
  145.  
  146. #ifdef USE_FUNCTIONS
  147.         if (GetMsg (reply)) {
  148.             /*
  149.              * Call user's function to do something to the icon.
  150.              */
  151.             (* ((void (*)()) ptr)) (win, (WORD) 0);
  152.             tr -> tr_time.tv_secs    = 0;
  153.             tr -> tr_time.tv_micro    =
  154.              (1000000L / TICKS_PER_SECOND);
  155.             SendIO (tr);
  156.         }
  157. #endif
  158.  
  159.         if (msg = GetMsg (win -> UserPort)) {
  160.             class = msg -> Class;
  161.             cs = msg -> Seconds;
  162.             cm = msg -> Micros;
  163.             ReplyMsg (msg);
  164.  
  165.             if (class == GADGETDOWN) {
  166.                 if (DoubleClick (secs, mics, cs, cm))
  167.                     break;
  168.                 secs = cs;  mics = cm;
  169.             }
  170.         }
  171.     }
  172.  
  173. #ifdef USE_FUNCTIONS
  174.     if (type == ICON_FUNCTION) {
  175.         AbortIO (tr);
  176.         WaitIO (tr);
  177.     }
  178. #endif
  179.  
  180.     *left = win -> LeftEdge;
  181.     *top = win -> TopEdge;
  182.     closestuff ();
  183.     return (1);
  184. }
  185.  
  186. static
  187. openstuff ()
  188. {
  189.     if (!(win = OpenWindow (&windef)))
  190.         return (0);
  191.     win -> UserData = (BYTE *) ICON;
  192.         
  193. #ifdef USE_FUNCTIONS
  194.     if (!(reply = CreatePort (NULL, NULL)) ||
  195.         !(tr = CreateExtIO (reply, (long) sizeof (*tr))) ||
  196.         OpenDevice (TIMERNAME, UNIT_VBLANK, tr, 0L)) {
  197.         closestuff ();
  198.         return (0);
  199.     }
  200. #endif
  201.  
  202.     return (1);
  203. }
  204.  
  205. static
  206. closestuff ()
  207. {
  208. #ifdef USE_FUNCTIONS
  209.     if (tr) {
  210.         if (tr -> tr_node.io_Device)
  211.             CloseDevice (tr);
  212.         DeleteExtIO (tr, (long) sizeof (*tr));
  213.     }
  214.     if (reply)        DeletePort (reply);
  215. #endif
  216.  
  217.     if (win)        CloseWindow (win);
  218. }
  219.